/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.bht.esa.weather.frontend;

import de.bht.esa.weather.model.WetterComForecast;
import de.bht.esa.weather.model.WetterComLocation;
import de.bht.esa.weather.model.geo.GeoCoordinates;
import de.bht.esa.weather.remotebeaninterfaces.WeatherAggregationRequest;
import de.bht.esa.weather.remotebeaninterfaces.WeatherResourceManager;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import org.codehaus.jettison.json.JSONObject;

/**
 * Stellt funktionen zur Abfrage der Wetterberichte via REST bereit.
 * 
 * @author Andreas Goese
 * @author Sven Höche
 */
@Path("weather")
public class WeatherRescource {

    WeatherResourceManager ressourceManager;
    
    @Context
    private UriInfo context;

    public WeatherRescource(@Context SecurityContext sc) throws Exception{
        InitialContext ctx = new InitialContext();
        this.ressourceManager = (WeatherResourceManager)ctx.lookup("de.bht.esa.weather.remotebeaninterfaces.WeatherResourceManager");
    }

    @GET
    @Produces("application/json")
    @Path("/search/{query}")
    @RolesAllowed({"ADMIN","USER"})
    public Response getRessourceUUID(@PathParam("query") String query) throws Exception{
        Map<String,Object> result = new HashMap<String, Object>();
        if(query.matches("^[-A-Za-z0-9ßäÄüÜöÖ ]{2,}$")){//Ort
            WetterComLocation[] locations = ressourceManager.searchLocation(query);
            if(locations == null || locations.length < 1){
                result.put("errormessage", "Location not found");
                return Response.status(Response.Status.PRECONDITION_FAILED).entity(new JSONObject(result)).build();
            }else{      
                result.put("ressourceId", ressourceManager.requestWeatherForecast(new WeatherAggregationRequest(query)));
                return Response.status(Response.Status.ACCEPTED).entity(new JSONObject(result)).build();
            }
        }else if(query.matches("^-{0,1}\\d{1,2}\\.\\d+,-{0,1}\\d{1,2}\\.\\d+$")){//Coordinates
            result.put("ressourceId", ressourceManager.requestWeatherForecast(new WeatherAggregationRequest(new GeoCoordinates(query))));
            return Response.status(Response.Status.ACCEPTED).entity(new JSONObject(result)).build();
        }else{
            result.put("errormessage", "Query does not match to given format");
            return Response.status(Response.Status.BAD_REQUEST).entity(new JSONObject(result)).build();
        }
    }
    
    @GET
    @Produces("application/json")
    @Path("/{resourceId}")
    @RolesAllowed({"ADMIN","USER"})
    public Response getRessource(@PathParam("resourceId") String resourceId){
        try {
            WetterComForecast forecast = ressourceManager.getForecast(resourceId);
            if (forecast != null) {
                return Response.ok(forecast).build();
            } else {
                return Response.status(Response.Status.NOT_MODIFIED).build();
            }
        } catch (EJBException ex) {
            return Response.status(Response.Status.NOT_FOUND).entity(ex.getCausedByException().getMessage()).build();
        }
    }
    
}
